package com.sx.ad.handler;

import com.alibaba.fastjson.JSON;
import com.sx.ad.dump.table.AdCreativeTable;
import com.sx.ad.dump.table.AdCreativeUnitTable;
import com.sx.ad.dump.table.AdPlanTable;
import com.sx.ad.dump.table.AdUnitTable;
import com.sx.ad.index.DataTable;
import com.sx.ad.index.IndexAware;
import com.sx.ad.index.adplan.AdPlanIndex;
import com.sx.ad.index.adplan.AdPlanObject;
import com.sx.ad.index.adunit.AdUnitIndex;
import com.sx.ad.index.adunit.AdUnitObject;
import com.sx.ad.index.creative.CreativeIndex;
import com.sx.ad.index.creative.CreativeObject;
import com.sx.ad.index.creativeunit.CreativeUnitIndex;
import com.sx.ad.index.creativeunit.CreativeUnitObject;
import com.sx.ad.mysql.constant.OpType;
import com.sx.ad.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * @author zkx
 * @Date 2024/9/14 18:43
 */
@Slf4j
public class AdLevelDataHandler {

	public static void handleLevel2(AdPlanTable planTable, OpType type){
		AdPlanObject adPlanObject = new AdPlanObject(
				planTable.getId(),
				planTable.getUserId(),
				planTable.getPlanStatus(),
				planTable.getStartDate(),
				planTable.getEndDate());
		handleBinlogEvent(
				DataTable.of(AdPlanIndex.class),
				adPlanObject.getPlanId(),
				adPlanObject,
				type);
	}

	public static void handleLevel2(AdCreativeTable creativeTable,
									OpType type) {

		CreativeObject creativeObject = new CreativeObject(
				creativeTable.getAdId(),
				creativeTable.getName(),
				creativeTable.getType(),
				creativeTable.getMaterialType(),
				creativeTable.getHeight(),
				creativeTable.getWidth(),
				creativeTable.getAuditStatus(),
				creativeTable.getAdUrl()
		);
		handleBinlogEvent(
				DataTable.of(CreativeIndex.class),
				creativeObject.getAdId(),
				creativeObject,
				type
		);
	}

	public static void handleLevel3(AdUnitTable unitTable, OpType type) {

		AdPlanObject adPlanObject = DataTable.of(
				AdPlanIndex.class
		).get(unitTable.getPlanId());
		if (null == adPlanObject) {
			log.error("handleLevel3 found AdPlanObject error: {}",
					unitTable.getPlanId());
			return;
		}

		AdUnitObject unitObject = new AdUnitObject(
				unitTable.getUnitId(),
				unitTable.getUnitStatus(),
				unitTable.getPositionType(),
				unitTable.getPlanId(),
				adPlanObject
		);

		handleBinlogEvent(
				DataTable.of(AdUnitIndex.class),
				unitTable.getUnitId(),
				unitObject,
				type
		);
	}

	public static void handleLevel3(AdCreativeUnitTable creativeUnitTable,
									OpType type) {

		if (type == OpType.UPDATE) {
			log.error("CreativeUnitIndex not support update");
			return;
		}

		AdUnitObject unitObject = DataTable.of(
				AdUnitIndex.class
		).get(creativeUnitTable.getUnitId());
		CreativeObject creativeObject = DataTable.of(
				CreativeIndex.class
		).get(creativeUnitTable.getAdId());

		if (null == unitObject || null == creativeObject) {
			log.error("AdCreativeUnitTable index error: {}",
					JSON.toJSONString(creativeUnitTable));
			return;
		}

		CreativeUnitObject creativeUnitObject = new CreativeUnitObject(
				creativeUnitTable.getAdId(),
				creativeUnitTable.getUnitId()
		);
		handleBinlogEvent(
				DataTable.of(CreativeUnitIndex.class),
				CommonUtils.stringConcat(
						creativeUnitObject.getAdId().toString(),
						creativeUnitObject.getUnitId().toString()
				),
				creativeUnitObject,
				type
		);
	}

	private static <K,V> void handleBinlogEvent(IndexAware<K,V> index,K key,V value, OpType type){
		switch (type) {
			case ADD:
				index.add(key,value);
				break;
			case UPDATE:
				index.update(key,value);
				break;
			case DELETE:
				index.delete(key,value);
				break;
			default:
				log.error("AOPLevelDataHandler error");
				break;
		}
	}

}
